package com.lwh.common.util;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;


/*** 日期工具类 ** @author zongrui**/
public class DateUtil {
	public static final String DATE_FORMAT_YYYY_MM_DD ="yyyy-MM-dd";
	public static final String DATE_FORMAT_YYYY_MM_DD_HH_MM_SS ="yyyy-MM-dd HH:mm:ss";
	public final static String FMT_YYYY = "yyyy";public final static String FMT_MMDD = "MMDD";
	public final static String FMT_MM_DD = "MM-DD";
	public final static String FMT_YYYY_MM = "yyyy-MM";
	public final static String FMT_YYYYMMDD = "yyyyMMdd";
	public final static String FMT_YYYY_MM_DD = "yyyy-MM-dd";
	public final static String FMT_YYYY_MM_DD_HH = "yyyy-MM-dd HH";
	public final static String FMT_YYYYMMDDHHMM = "yyyyMMddHHmm";
	public final static String FMT_YYYY_MM_DD_HH_MM = "yyyy-MM-dd-HH:mm";
	public final static String FMT_MM_DD_HH_MM_SS = "MM-dd-HH mm:ss";
	static GregorianCalendar gc = new GregorianCalendar();
	static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

	/*** 字符串转换为日期格式
	 * @param strDate
	 * @param pattern
	 * @return*/
	public static Date stringToDate(String strDate, String pattern) {
		try {
			return new SimpleDateFormat(pattern).parse(strDate);
		} catch (Exception e) {
			return null;
		}}
	/*** 日期格式转换为字符串
	 * @param date
	 * @param pattern
	 @return*/
	public static String dateToString(Date date, String pattern) {
		try {
			if (date == null)return null;return new SimpleDateFormat(pattern).format(date);
		} catch (Exception e) {return null;}}
	/*** 获得指定格式的当前日期
	 * @param pattern* @return
	 */
	public static String getCurrentDate(String pattern) {
		return new SimpleDateFormat(pattern).format(new Date());
	}
	/*** 计算两段日期之间的天数
	 * @param beginDate
	 * @param endDate* @return*/
	public static long dateDayPoor(Date beginDate, Date endDate) {
		long days = 0;if (beginDate == null || endDate == null)return days;
		Calendar c1 = Calendar.getInstance();c1.setTime(beginDate);
		Calendar c2 = Calendar.getInstance();c2.setTime(endDate);
		long l1 = c1.getTimeInMillis();long l2 = c2.getTimeInMillis();
		// 计算天数
		days = (l2 - l1) / (24 * 60 * 60 * 1000);
		return days;
	}
	/*** 取得某时间后n天,格式为yyyy-mm-dd
	 * @param date
	 * @param n* @return*/
	@SuppressWarnings("static-access")
	public static String getNDayAfter(Date date, int n){
		Calendar c = Calendar.getInstance();c.setTime(date);
		c.add(c.DAY_OF_MONTH, n);
		return ""+c.get(c.YEAR)+"-"+(c.get(c.MONTH)+1)+"-"+c.get(c.DATE);
	}
	/*** 取得某时间前n天,格式为yyyy-mm-dd
	 * @param date
	 * @param n
	 * @return*/
	@SuppressWarnings("static-access")public static String getNDayBefore(Date date, int n){
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(c.DAY_OF_MONTH, -n);
		return ""+c.get(c.YEAR)+"-"+(c.get(c.MONTH)+1)+"-"+c.get(c.DATE)+" "+c.get(c.HOUR_OF_DAY)+":"+c.get(c.MINUTE);
	}
	/*** 比较日期大小，dt1>dt2返回1，dt1=dt2返回0，dt1 dt2.getTime()) {
	 return 1;} else if (dt1.getTime() == dt2.getTime()) {return 0;} else {return -1;}}/*** 根据日期返回星期 * @param date* @return*/public static String getWeekOfDate(Date date) {if (date == null) return "";String[] weekDays = { "7", "1", "2", "3", "4", "5", "6" };Calendar cal = Calendar.getInstance();cal.setTime(date);int w = cal.get(Calendar.DAY_OF_WEEK) - 1;if (w < 0) {w = 0;}return weekDays[w];}/** 返回 d1 - d2 之间相差的天数 */ public static int days_between(Date d1, Date d2) {int elapsed = 0;GregorianCalendar gc1, gc2;GregorianCalendar g1 = new GregorianCalendar();GregorianCalendar g2 = new GregorianCalendar();g1.setTime(d1);g2.setTime(d2);int zhenfu = 1;if (g2.after(g1)) {gc2 = (GregorianCalendar) g2.clone();gc1 = (GregorianCalendar) g1.clone();zhenfu = -1;} else {gc2 = (GregorianCalendar) g1.clone();gc1 = (GregorianCalendar) g2.clone();}gc1.clear(Calendar.MILLISECOND);gc1.clear(Calendar.SECOND);gc1.clear(Calendar.MINUTE);gc1.clear(Calendar.HOUR_OF_DAY);gc2.clear(Calendar.MILLISECOND);gc2.clear(Calendar.SECOND);gc2.clear(Calendar.MINUTE);gc2.clear(Calendar.HOUR_OF_DAY);while (gc1.before(gc2)) {gc1.add(Calendar.DATE, 1);elapsed++;}return elapsed * zhenfu;}// 得到日期的星期 public static String getDayOfWeek(String date) {String str = "";Calendar c = parseCalendar(date);if (c==null) return str;int day = c.get(Calendar.DAY_OF_WEEK);switch (day) {case Calendar.SUNDAY:str = "日"; break;case Calendar.MONDAY:str = "一"; break;case Calendar.TUESDAY:str = "二"; break;case Calendar.WEDNESDAY:str = "三"; break;case Calendar.THURSDAY:str = "四"; break;case Calendar.FRIDAY:str = "五"; break;case Calendar.SATURDAY:str = "六"; break;default:str = "";}return str;}public static Calendar parseCalendar(String s) {Calendar c = null;Date d = parseDate(s);if (d != null) {c = Calendar.getInstance();c.setTime(d);}return c;}/** 解析字符串中的日期 */ public static Date parseDate(String s) {Date d = null;try {d = sdf.parse(s);} catch (Exception e) {}return d;}/*** 获取某时间后n天的时间 * @param date* @param n* @return*/@SuppressWarnings("static-access")public static Date getNDayTimeAfter(Date date, int n){Calendar calendar = Calendar.getInstance();calendar.setTime(date);calendar.add(calendar.DAY_OF_MONTH, n);return calendar.getTime();}/*** 获取某时间前n天的时间 * @param date* @param n* @return*/public static Date getNDayTimeBefore(Date date, int n){Calendar c = Calendar.getInstance();c.setTime(date);c.add(Calendar.DAY_OF_MONTH, -n);return c.getTime();}/** 例如：addDate(new Date(), UserDate.DAY, 10) -- 在当前日期上加10天 */ public static Date addDate(Date date, int field, int amount) {gc.setTime(date);gc.add(field, amount);return gc.getTime();}public static void main(String[] args) {System.out.println(DateUtil.getCurrentDate(DATE_FORMAT_YYYY_MM_DD_HH_MM_SS));}/*** 获取指定时间的上一天 * @author Lrain* @param time* @return* 2012-3-16*/public static Date returnPreviousDay(Date time){Calendar calendar = Calendar.getInstance();calendar.setTime(time);calendar.set(Calendar.DAY_OF_MONTH,calendar.get(Calendar.DAY_OF_MONTH) - 1);calendar.get(Calendar.DAY_OF_MONTH);return calendar.getTime();}/*** 获取指定时间的下一天 * @author Lrain* @param time* @return* 2012-3-16*/public static Date returnNextDay(Date time){Calendar calendar = Calendar.getInstance();calendar.setTime(time);calendar.set(Calendar.DAY_OF_MONTH,calendar.get(Calendar.DAY_OF_MONTH) + 1);calendar.get(Calendar.DAY_OF_MONTH);return calendar.getTime();}/**** [获取当前时间的年份最后两位 ，例如2015获取15]** @comment [注释说明]* @author whp* @date 2015-1-24 下午1:37:03** @return*/public static String getYearLast(Date date){String yearLast = new SimpleDateFormat("yy",Locale.CHINESE).format(date);return yearLast;} }
}
